package com.kanqiubao.kq.utils.sharepreferences;

import java.io.IOException;

import android.content.SharedPreferences;

import com.kanqiubao.kq.ThisAppApplication;
import com.kanqiubao.kq.utils.JavaBase64;
import com.kanqiubao.kq.utils.StringUtil;

/**
 * 缓存数据
 *
 * @author dennis
 */
public class SharedPreferencesUtil {

    private static SharedPreferences mSharedPreferences;

    private static SharedPreferencesUtil mSharedPreferencesUtil;

    private SharedPreferencesUtil() {
    }

    public static SharedPreferencesUtil getInstance() {
        if (mSharedPreferencesUtil == null) {
            mSharedPreferencesUtil = new SharedPreferencesUtil();
        }
        if (mSharedPreferences == null) {
            mSharedPreferences = new SecurePreferences(ThisAppApplication.getInstance());
        }
        return mSharedPreferencesUtil;
    }

    public void loginOut() {
        setToken("");
        setIsLogin(false);
        setUserFace("");
        setUserNick("");
    }

    /**
     * 根据token获取用户id
     *
     * @return
     */
    public int getUserId() {
        String userId = getToken();
        if (!StringUtil.isEmptyString(userId)) {
            try {
                String temp = new String(JavaBase64.decode(userId));
                if (temp.contains("-")) {
                    String[] st = temp.split("-");
                    return Integer.valueOf(st[0]);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    public void setIsLogin(boolean bool) {
        mSharedPreferences.edit().putBoolean("islogin", bool).commit();
    }

    public boolean getIsLogin() {
        return mSharedPreferences.getBoolean("islogin", false);
    }


    public void setToken(String key) {
        mSharedPreferences.edit().putString("token", key).commit();
    }

    public String getToken() {
        return mSharedPreferences.getString("token", "");
    }

    public void setUserName(String name) {
        mSharedPreferences.edit().putString("uname", name).commit();
    }

    public String getUserName() {
        return mSharedPreferences.getString("uname", "");
    }

    public void setUserType(int name) {
        mSharedPreferences.edit().putInt("uname_type", name).commit();
    }

    public void setUserFace(String pw) {
        mSharedPreferences.edit().putString("userface", pw).commit();
    }

    public String getUserFace() {
        return mSharedPreferences.getString("userface", "");
    }

    public void setUserNick(String nick) {
        mSharedPreferences.edit().putString("usernick", nick).commit();
    }

    public String getUserNick() {
        return mSharedPreferences.getString("usernick", "");
    }


    public void setSearchLive(String nick) {
        mSharedPreferences.edit().putString("search_live_his", nick).commit();
    }

    public String getSearchLive() {
        return mSharedPreferences.getString("search_live_his", "");
    }

    public void setSearchRecoding(String nick) {
        mSharedPreferences.edit().putString("search_recoding_his", nick).commit();
    }

    public String getSearchRecoding() {
        return mSharedPreferences.getString("search_recoding_his", "");
    }

    public void setSearchVideo(String nick) {
        mSharedPreferences.edit().putString("search_video_his", nick).commit();
    }

    public String getSearchVideo() {
        return mSharedPreferences.getString("search_video_his", "");
    }


}
